package com.yanjr.modules.ad.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yanjr.common.persistence.Page;
import com.yanjr.common.utils.Global;
import com.yanjr.common.utils.StringUtils;
import com.yanjr.common.web.BaseController;
import com.yanjr.modules.ad.entity.AdSpace;
import com.yanjr.modules.ad.entity.Advertisement;
import com.yanjr.modules.ad.service.AdSpaceService;
import com.yanjr.modules.ad.service.AdvertisementService;

/**
 * 广告Controller 完成对广告 的增加，删除，修改，查看<br>
 * 对应jsp页面在 /WEB-INF/views/modules/advertise 目录下<br>
 * 其中所要注意的功能如下：<br>
 *
 * @ClassName: AdvertisementController
 * @author lipingping
 * @date 2015年2月03日 上午9:17:33
 * @since 0.2 @version 0.3
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adver")
public class AdvertisementController extends BaseController {

	@Autowired
	private AdvertisementService advertisementService;

	@Autowired
	private AdSpaceService adSpaceService;

	/**
	 * 
	 * 根据id获得广告
	 * 
	 * @param id
	 * @return Advertisement
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@ModelAttribute
	public Advertisement get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return advertisementService.get(id);
		} else {
			return new Advertisement();
		}
	}

	/**
	 * 
	 * 获取广告列表
	 * 
	 * @param model
	 *            返回模型
	 * @return modules/" + "advertise/advertisementList
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value={"list", ""})
	public String list(@RequestParam(required = false) String spaceId,
			Advertisement advertisement, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// 从session中获取spaceid
//		if (StringUtils.isEmpty(spaceId)) {
//			spaceId = AdUtils.getSpaceId();
//		} else {
//			AdUtils.setSpaceId(spaceId);
//		}

		Page<Advertisement> page = advertisementService.find(new Page<Advertisement>(request,
				response), advertisement);

		model.addAttribute("page", page);
		return "modules/advertise/advertisementList";
	}
	
	@RequestMapping(value={"listtoview"})
	public String listtoview(Model model) {

		List<Advertisement> list = advertisementService.findAllEnable("51db08a496e342e0bed3e9e9c27f50a4");
		for(Advertisement ad : list) {
			System.out.println("name:" + ad.getName() + " adcon_path:" + ad.getAdconPath() + " url:" + ad.getUrl());
		}
		model.addAttribute("list", list);
		return "modules/advertise/showimg";
	}
	
	@RequestMapping(value = { "adalllist" })
	public String adalllist(@RequestParam(required = false) String spaceId,
			Advertisement advertisement, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<Advertisement> page = null;

		if (!StringUtils.isEmpty(spaceId)) {
			AdSpace adSpace = adSpaceService.get(spaceId);

			if (null == adSpace) {
				page = new Page<Advertisement>(request, response);
			} else {
				page = advertisementService.find(new Page<Advertisement>(
						request, response), spaceId, advertisement);
			}

		} else {
			page = advertisementService.find(new Page<Advertisement>(request,
					response), advertisement);
		}

		model.addAttribute("page", page);
		return "modules/" + "advertise/advertisementList";
	}

	@RequestMapping(value = { "treeData" })
	public String data(Model model) {
		// List<Map<String, Object>> mapList = Lists.newArrayList();
		List<AdSpace> spacels = adSpaceService.allData();
		model.addAttribute("spacels", spacels);
		return "modules/" + "advertise/adspaceTree";
	}

	@RequestMapping(value = { "adindex", "" })
	public String adindex() {
		return "modules/" + "advertise/adIndex";
	}

	/**
	 * 
	 * 查看广告内容
	 * 
	 * @param model
	 *            返回模型
	 * @return "modules/" + "advertise/advertisementForm
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value = "form")
	public String form(Advertisement advertisement, Model model) {
//		String spaceId = AdUtils.getSpaceId();
//		if (!StringUtils.isEmpty(spaceId)) {
//			advertisement.setSpace(adSpaceService.get(spaceId));
//		}
		model.addAttribute("advertisement", advertisement);
		List<AdSpace> spacels = adSpaceService.allData();
		model.addAttribute("spacels", spacels);
		return "modules/" + "advertise/advertisementForm";
	}

	/**
	 * 
	 * 保存广告内容
	 * 
	 * @param model
	 *            返回模型
	 * @return 
	 *         redirect:"+Global.getAdminPath()+"/myadvertise/myadvertisement/list?repage
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value = "save")
	public String save(Advertisement advertisement, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, advertisement)) {
			return form(advertisement, model);
		}
		
		advertisementService.save(advertisement);
		addMessage(redirectAttributes, "保存广告'" + advertisement.getName()
				+ "'成功");
		return "redirect:" + Global.getAdminPath() + "/ad/adver/list?repage";
	}

	/**
	 * 
	 * 删除广告内容
	 * 
	 * @param model
	 *            返回模型
	 * @return 
	 *         redirect:"+Global.getAdminPath()+"/myadvertise/myadvertisement/list?repage
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		advertisementService.delete(id);
		addMessage(redirectAttributes, "删除广告成功");
		return "redirect:" + Global.getAdminPath()
				+ "/myadvertise/myadvertisement/list?repage";
	}

	@RequestMapping(value = "enable")
	public String enableSpage(String id, RedirectAttributes redirectAttributes) {
		advertisementService.enableById(id);
		addMessage(redirectAttributes, "启用广告成功");
		return "redirect:" + Global.getAdminPath()
				+ "/ad/adver/list?repage";
	}

	@RequestMapping(value = { "flashUpload" })
	public String flashUpload(
			@RequestParam(required = false) MultipartFile flashfile, Model model) {
//		if (flashfile != null) {
//			String uploadurl = fileUploadUtils.videoFileUpload(flashfile,
//					Constants.RESOURCE_VIDEO, model);
//			model.addAttribute("flashpath", uploadurl);
//		}
		return "modules/" + "advertise/flashiframe";
	}

	@RequestMapping(value = "saveTopLevel")
	public String saveTopLevel(String[] wids, String[] orders,
			RedirectAttributes redirectAttributes) {

		try {
			for (int i = 0; i < wids.length; i++) {
				Advertisement advertisement = advertisementService.get(wids[i]);
				advertisement.setOrderId(new Integer(orders[i]));
				advertisementService.save(advertisement);

			}
			addMessage(redirectAttributes, "更新排序成功");

		} catch (Exception e) {
			addMessage(redirectAttributes, "排序必须为数字");
		}

		return "redirect:" + Global.getAdminPath()
				+ "/myadvertise/myadvertisement/list?repage";
	}

}
